import { getStorage, setStorage } from "../../../../utils/storage"
import { userBehavior } from "./behavior"
import { UpdataUserInfo } from '../../../../pages/api/userInfo'
import { toast } from '../../../../utils/extendAPI'
Page({
  behaviors: [userBehavior],
  // 页面的初始数据
  data: {
    isShowPopup: false // 控制更新用户昵称的弹框显示与否
  },
  chooseAvatar(event) {
    const { avatarUrl } = event.detail
    wx.uploadFile({
      url: 'https://gmall-prod.atguigu.cn/mall-api/fileUpload',
      filePath: avatarUrl,
      name: 'file',
      header: {
        token: getStorage('token')
      },
      success: (res) => {
        const uploadRes = JSON.parse(res.data)
        console.log(uploadRes);
        this.setData({
          'userInfo.headimgurl': uploadRes.data
        })
      }
    })
  },
  async save() {
    const res = await UpdataUserInfo(this.data.userInfo)
    console.log(res);
    if (res.code === 200) {
      setStorage('userInfo', this.data.userInfo)
      this.getUserInfo(this.data.userInfo)
      toast()
    }
  },
  //修改用户名
  getNickname(event) {
    const { nickname } = event.detail.value
    console.log(nickname);
    this.setData({
      'userInfo.nickname': nickname,
      isShowPopup: false
    })
  },
  // 显示修改昵称弹框
  onUpdateNickName() {
    this.setData({
      isShowPopup: true,
      'userInfo.nickname': this.data.userInfo.nickname
    })
  },

  // 弹框取消按钮
  cancelForm() {
    this.setData({
      isShowPopup: false
    })
  }
})
